import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;
import java.util.Scanner;

public class JDBCTest3 {
    //删除操作
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java100?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1234");

        Connection connection = dataSource.getConnection();

        //拼装SQL
        //删除学生id
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要删除的学生id:");
        int id = scanner.nextInt();
        String sql = "delete from student where id=?";

        //把这个字符串转化为一个专门的对象
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);


        //执行SQL
        //插入, 删除, 修改, 都是 executeUpdate
        int ret = statement.executeUpdate();
        System.out.println("ret="+ret);

        statement.close();
        connection.close();
    }
}
